﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AdminUser_Manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateDropdownlist();
            PopulateAdminUser();
        }
    }
    public void PopulateDropdownlist()
    {
        drpAdminRole.DataSource = AdminRole.GetAll();
        drpAdminRole.DataTextField = "RoleName";
        drpAdminRole.DataValueField = "ID";
        drpAdminRole.DataBind();
        drpAdminRole.Items.Insert(0, new ListItem("请选择", "0"));
        drpAddAdminRole.DataSource = AdminRole.GetAll();
        drpAddAdminRole.DataTextField = "RoleName";
        drpAddAdminRole.DataValueField = "ID";
        drpAddAdminRole.DataBind();
    }

    public void PopulateAdminUser()
    {
        lvAdminUser.DataSource = AdminUser.GetAllByConditon(DongDong.Common.Misc.ConvertToInt(drpAdminRole.SelectedValue), txtTimeFrom.Text, txtTimeTo.Text, txtKey.Text);
        lvAdminUser.DataBind();
    }

    protected void lvAdminUser_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lvAdminUser.EditIndex = -1;
        PopulateAdminUser();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        PopulateAdminUser();
    }

    protected void lvAdminUser_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int ID = DongDong.Common.Misc.ConvertToInt(lvAdminUser.DataKeys[e.ItemIndex].Value);
        AdminUser.Delete(ID);
        PopulateAdminUser();
    }
    protected void lvAdminUser_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lvAdminUser.EditIndex = e.NewEditIndex;
        PopulateAdminUser();

        int ID = DongDong.Common.Misc.ConvertToInt(lvAdminUser.DataKeys[e.NewEditIndex].Value);
        DropDownList drpEditRole = (DropDownList)lvAdminUser.Items[e.NewEditIndex].FindControl("drpEditRole");
        drpEditRole.DataSource = AdminRole.GetAll();
        drpEditRole.DataTextField = "RoleName";
        drpEditRole.DataValueField = "ID";
        drpEditRole.DataBind();
        AdminUser obj = AdminUser.Get(ID);
        if (AdminRole.GetAll().Select(row => row.ID).Contains(obj.RoleID))
        {
            drpEditRole.SelectedValue = obj.RoleID.ToString();
        }

    }
    protected void lvAdminUser_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        var item = lvAdminUser.Items[e.ItemIndex];
        int ID = DongDong.Common.Misc.ConvertToInt(lvAdminUser.DataKeys[e.ItemIndex].Value);
        AdminUser obj = AdminUser.Get(ID);
        DropDownList drpEditRole = (DropDownList)lvAdminUser.Items[e.ItemIndex].FindControl("drpEditRole");
        TextBox txtEdiName = (TextBox)item.FindControl("txtEdiName");
        TextBox txtEditMiMa = (TextBox)item.FindControl("txtEditMiMa");

        obj.RoleID = DongDong.Common.Misc.ConvertToInt(drpEditRole.SelectedValue);
        if (obj.UserName != txtEdiName.Text)
        {
            AdminUser objAdminUser = AdminUser.GetAdminUserByUsername(txtEdiName.Text);
            if (objAdminUser != null)
            {
                DongDong.Common.MessageBox.Show(this,"该用户已经存在,请重新修改");
                return;
            }
        }
        obj.UserName = txtEdiName.Text;
        obj.Password = txtEditMiMa.Text;
        obj.Update();
        lvAdminUser.EditIndex = -1;
        PopulateAdminUser();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        AdminUser obj = AdminUser.GetAdminUserByUsername(txtAddName.Text);
        if (obj != null)
        {
            DongDong.Common.MessageBox.Show(this, "该用户已经存在,请重新修改");
            return;
        }
        obj = new AdminUser();
        obj.RoleID = DongDong.Common.Misc.ConvertToInt(drpAddAdminRole.SelectedValue);
        obj.UserName = txtAddName.Text;
        obj.Password = txtAddPassWord.Text;
        obj.LoginTime = System.DateTime.Now;
        obj.LastLoginTime = System.DateTime.Now;
        obj.Add();
        DongDong.Common.MessageBox.ShowAndRedirect(this, "添加成功", "Manage.aspx");
        PopulateAdminUser();
    }

    public string GetAdiminRolNameByRoleID(object RoleID)
    {
        string Title = "该角色被删除";
        AdminRole obj = AdminRole.Get(DongDong.Common.Misc.ConvertToInt(RoleID));
        if (obj != null)
            Title = obj.RoleName;
        return Title;
    }

}